package com.gewu.pm.ui.dialog;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.StringRes;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.hjq.base.BaseDialog;

/**
 * 填写说明弹窗
 */
public class FillDescriptionDialog {
    
    public static final class Builder extends BaseDialog.Builder<Builder> {

        private OnListener mListener;
        private final TextView tvTitle;
        private final EditText etDescription;
        private final Button btCancel;
        private final Button btConfirm;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.fill_description_dialog);
            setAnimStyle(BaseDialog.ANIM_SCALE);
            setCanceledOnTouchOutside(false);
            
            tvTitle = findViewById(R.id.tv_title);
            etDescription = findViewById(R.id.et_description);
            btCancel = findViewById(R.id.bt_cancel);
            btConfirm = findViewById(R.id.bt_confirm);
            
            setOnClickListener(btCancel, btConfirm);
        }

        /**
         * 设置标题
         */
        public Builder setTitle(@StringRes int id) {
            return setTitle(getString(id));
        }

        public Builder setTitle(CharSequence text) {
            tvTitle.setText(text);
            return this;
        }

        /**
         * 设置说明内容
         */
        public Builder setDescription(CharSequence text) {
            etDescription.setText(text);
            return this;
        }

        /**
         * 获取说明内容
         */
        public String getDescription() {
            return etDescription.getText().toString().trim();
        }

        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        @SingleClick
        @Override
        public void onClick(View v) {
            int viewId = v.getId();
            if (viewId == R.id.bt_confirm) {
                if (mListener != null) {
                    String description = getDescription();
                    mListener.onConfirm(getDialog(), description);
                }
                dismiss();
            } else if (viewId == R.id.bt_cancel) {
                if (mListener != null) {
                    mListener.onCancel(getDialog());
                }
                dismiss();
            }
        }
    }

    public interface OnListener {
        /**
         * 确定
         */
        void onConfirm(BaseDialog dialog, String description);

        /**
         * 取消
         */
        default void onCancel(BaseDialog dialog) {}
    }
}
